|
|
|
|
|
|
|
File-Manipulation Functions |
|
|
|
|
|
|
|
|
As with the Dir$ function, most of the file-manipulation commands in Visual Basic are as straightforward as their MS-DOS equivalents, although with a few limitations. These commands are summarized in Table 21.2. |
|
|
|
|
| Table 21.2 Summary of File Functions | | Action | Syntax | | Copy a file | FileCopy source, dest | | Delete one or more files | Kill path | | Rename a file | Name oldname As newname | | Create a new folder | MkDir pathname | | Remove an empty folder | RmDir pathname | | Change current directory | ChDir pathname | | Change current drive | ChDrive drive |
|
|
|
|
|
|
Several of these system functions are described in the following sections. |
|
|
|
|
|
|
|
|
The FileCopy command has the limitation that you cannot use wildcards to specify multiple files. FileCopy can copy files locally or over a network, as shown in the following examples: |
|
|
|
|
|
|
|
|
'The following line copies a file while changing its name:
FileCopy "D:\My Documents\Hey Now.txt", "C:\DATA\TEST.TXT"
'The following lines of code use a network path for the source file:
Dim sDest As String
Dim sSource As String
SSource = "\\myserver\deptfiles\budget98.XLS"
SDest = "C:\DATA\BUDGET.XLS"
FileCopy sSouce, sDest |
|
|
|
|
|
|
|
|
The FileCopy statement automatically overwrites an existing file, unless the file is read-only or locked open by another application. |
|
|
|
|
|
|
|
|
|
When you copy files over a network or dial-up connection, using the On Error statement to trap errors is a good idea. For example, if the network is down or another user has the file exclusively locked, your program could bomb unexpectedly. You can easily provide an error trap and retry dialog, as described in Chapter 9, "Visual Basic Programming Fundamentals."  |
|
|
|
|
|
|
|
|
|
|
See ''Using the On Error Statement" p. 219 |
|
|
|
|
|